//
//  ZBCache.m
//  ZBDay
//
//  Created by xzb on 2018/2/25.
//  Copyright © 2018年 xzb. All rights reserved.
//

#import "ZBCache.h"
#import "YYCache.h"

@interface ZBCache ()

@property (nonatomic, strong) YYCache *yyCache;

@end

@implementation ZBCache

+ (instancetype)instance
{
    static dispatch_once_t oncePredicate;
    static id instance;
    dispatch_once(&oncePredicate, ^{
        instance = [[self alloc] init];
    });
    return instance;
}

- (instancetype)init
{
    self = [super init];
    if (self) {
        _yyCache = [[YYCache alloc] initWithName:@"com.zhibin_user.www"];
    }
    return self;
}

- (void)clearCache
{
    [self.yyCache removeAllObjects];
}

- (void)setData:(NSData *)data forKey:(NSString *)cacheKey
{
    [self.yyCache setObject:data forKey:cacheKey];
}

- (NSData *)dataForKey:(NSString *)cacheKey
{
    return (NSData *) [self.yyCache objectForKey:cacheKey];
}

- (BOOL)containsObjectForKey:(NSString *)key
{
    return [self.yyCache containsObjectForKey:key];
}

- (void)removeObjectForKey:(NSString *)key
{
    [self.yyCache removeObjectForKey:key];
}

- (void)removeAllObjects
{
    [self.yyCache removeAllObjects];
}

+ (void)onceEventForKey:(NSString *)key event:(void(^)(void))event
{
    if (![[ZBCache instance] containsObjectForKey:key]) {
        [[ZBCache instance] setData:[NSData dataWithContentsOfURL:[NSURL URLWithString:@"xzb"]] forKey:key];
        BLOCK_EXEC(event);
    }
}


@end

